﻿
Const BLEND_ALPHA:Int		= 1
Const BLEND_MULTIPLY:Int	= 2
Const BLEND_ADD:Int			= 3
Const BLEND_COLOR:Int		= 4
Const BLEND_REPLACE:Int		= 5

Function GetEntityBrush:Int(ent:Int)
	Local brush:Int 	= 0
	Local mat:Material	= New Material
	
	Local obj:B3DEntity = B3DEntity(HandleToObject(ent))
	If obj._typ = ET_ENTITY
		mat = Entity(obj.obj).GetMaterial()
		brush = HandleFromObject(mat)	
	End If
	
	Return brush
End Function


Function CopyEntityBrush:Int(ent:Int)
	Local brush:Int = 0
	Local newBrushName:String = getBlitz3D()._root.genName("brush_")
	Local e:B3DEntity = B3DEntity(HandleToObject(ent))
	If e._typ = ET_ENTITY
		Local mat:Material	= Entity(e.obj).GetMaterial().clone(newBrushName)
		mat._materialName	= newBrushName
		
		brush = HandleFromObject(mat)
	EndIf
	
	Return brush
End Function


Function GetSurfaceBrush:Int(ent:Int, surface:Int)
	Local brush:Int 	= 0
	Local mat:Material	= New Material
	Local obj:B3DEntity	= B3DEntity(HandleToObject(ent))
	
	If obj._typ = ET_ENTITY
		mat = Entity(obj.obj).GetMaterial(surface)
		brush = HandleFromObject(mat)	
	End If
	
	Return brush
End Function


Function PaintEntity(ent:Int, brush:Int)
	Local obj:B3DEntity = B3DEntity(HandleToObject(ent))
	Entity(obj.obj).setMaterialName(Material(HandleToObject(brush))._materialName)
End Function


Function BrushColor(brush:Int, r:Float, g:Float, b:Float)
	Local _r:Float = (1.0 / 255.0) * r
	Local _g:Float = (1.0 / 255.0) * g
	Local _b:Float = (1.0 / 255.0) * b
	Material(HandleToObject(brush)).setDiffuse(r, g, b, 1.0)
End Function


Function BrushShininess(brush:Int, shininess:Float)
	Material(HandleToObject(brush)).setShininess(shininess)
End Function


Function BrushAlpha(brush:Int, a:Float)
	Local mat:Material = Material.getFromHandle(brush)
	If mat._color = Null Then mat._color = ColourValue.Create(1.0, 1.0, 1.0, a) Else mat._color.Set(mat._color.r, mat._color.g, mat._color.b, a)
	
	mat.setDiffuse(mat._color.r, mat._color.g, mat._color.b, mat._color.a)
End Function

rem
bbdoc: Sets the blending mode for a brush.
about: <p><b>BlendModes</b></p>
<table cellpadding="4" cellspacing="4" border="0">
<tr><td><b>Const</b></td><td><b>Value</b></td><td><b>Ogre3D correspondance</b></td></tr>
<tr><td>BLEND_ALPHA (default)</td><td>1</td><td>SBT_TRANSPARENT_ALPHA</td></tr>
<tr><td>BLEND_MULTIPLY</td><td>2</td><td>SBT_MODULATE</td></tr>
<tr><td>BLEND_ADD</td><td>3</td><td>SBT_ADD</td></tr>
<tr><td>BLEND_COLOR</td><td>4</td><td>SBT_TRANSPARENT_COLOUR</td></tr>
<tr><td>BLEND_REPLACE</td><td>5</td><td>SBT_REPLACE</td></tr>
</table>
endrem
Function BrushBlend(brush:Int, blend:Int = 1)
	Local blendmode:Int
	Select blend
		Case 2	; blendmode = SBT_MODULATE
		Case 3	; blendmode = SBT_ADD
		Case 4	; blendmode = SBT_TRANSPARENT_COLOUR
		Case 5	; blendmode = SBT_REPLACE
		Default ; blendmode = SBT_TRANSPARENT_ALPHA
	End Select
	
	Material.getFromHandle(brush).setSceneBlending(blendmode)
End Function


Const BFX_NOTHING:Int					= 0
Const BFX_FULLBRIGHT:Int				= 1
Const BFX_VERTEX_COLORS:Int				= 2
Const BFX_FLATSHADED:Int				= 4
Const BFX_DISABLE_FOG:Int				= 8
Const BFX_NO_BACKFACE_CULLING:Int		= 16
Const BFX_TRANSPARENCY_CASTS_SHADOWS:Int= 32

Function BrushFX(brush:Int, flags:Int)
	Local mat:Material = Material.getFromHandle(brush)
	
	If flags & BFX_FULLBRIGHT Then mat.setSelfIllumination(1.0, 1.0, 1.0) Else mat.setSelfIllumination(0.0, 0.0, 0.0)
	If flags & BFX_VERTEX_COLORS Then flags = 0
	If flags & BFX_FLATSHADED Then mat.setShadingMode(SO_FLAT) Else mat.setShadingMode(SO_GOURAUD)
	If flags & BFX_DISABLE_FOG Then mat.setFog(False) Else mat.setFog(False, FOG_EXP)
	If flags & BFX_NO_BACKFACE_CULLING Then mat.setCullingMode(CULL_NONE) Else mat.setCullingMode(CULL_CLOCKWISE)
End Function


Function BrushName:String(brush:Int)
	Return Material.getFromHandle(brush).getName()
End Function


Function GetBrushByName:Int(brush_name:String)
	Return HandleFromObject(MaterialManager.getByName(brush_name))
End Function


Function GetBrushTechnique:Int(brush:Int, index:Int = 0)
	Local bt:Int = 0
	Local b:Material = Material(HandleToObject(brush))
	If b <> Null Then bt = HandleFromObject(b.getBestTechnique(index))
	
	Return bt
End Function


Function GetBrushPass:Int(tech:Int, passIndex:Int = 0)
	Local p:Int = 0
	Local t:Technique = Technique(HandleToObject(tech))
	If t <> Null
		p = HandleFromObject(t.getPass(passIndex))
	End If
	
	Return p
End Function


Function GetPassPars:Int(brush_pass:Int)
	Local pars:Int = 0
	Local bp:Pass = Pass(HandleToObject(brush_pass))
	If bp <> Null
		pars = HandleFromObject(bp.getFragmentProgramParameters())
	End If
	
	Return pars
End Function


Function SetBrushConstantF(fppars:Int, par_name:String, par_value:Float)
	Local p:GpuProgramParameters = GpuProgramParameters(HandleToObject(fppars))
	If p <> Null Then p.setNamedConstant(par_name, par_value)
End Function


Function CompileBrush(brush:Int)
	Material(HandleToObject(brush)).Compile()
End Function


Function UpdateBrushPass(brush_pass:Int)
	pass(handletoobject(brush_pass)).processPendingPassUpdates()
End Function